﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Data;

namespace cs499.Database
{
    public partial class Faculty
    {
        public bool Delete()
        {
            bool retval = true;
            Model1Container db = new Model1Container();
            Faculty faculty = db.Set<Faculty>()
                .Include("PreferredInstances")
                .Include("CourseInstances")
                .FirstOrDefault(Facultytofind => Facultytofind.aspnet_userID == this.aspnet_userID);
            
            foreach (CourseInstance ci in faculty.PreferredInstances.ToList())
            {
                db.Set<CourseInstance>().Remove(ci);
            }
            foreach (CourseInstance ci in faculty.CourseInstances.ToList())
            {
                db.Set<CourseInstance>().Remove(ci);
            }
            try
            {
                MembershipUser tempuser = Membership.GetUser(faculty.aspnet_userID);
                if (tempuser == null) retval = false;
                else retval = Membership.DeleteUser(tempuser.UserName);
                
                db.Set<Faculty>().Remove(faculty);
                db.SaveChanges();
                
            }
            catch (Exception /**/)
            {
                return false;
            }
            return retval;
        }

        public List<Faculty> GetFacultyForDepartment(Int32 departmentId)
        {
            //DataSet retset;
            Model1Container db = new Model1Container();
            return db.Set<Faculty>().Where(Facultytofind => Facultytofind.DepartmentID == departmentId).ToList();
            //return retset;
        }

        public bool CreatePreference(int courseid, int roomid, int timeid)
        {

            Model1Container db = new Model1Container();
            Faculty currentfac = db.Set<Faculty>().FirstOrDefault(fac => fac.Id == this.Id);
            Course course = db.Set<Course>().FirstOrDefault(cr => cr.Id == courseid);
            Room room = db.Set<Room>().FirstOrDefault(rm => rm.Id == roomid);
            TimeSlot timeslot = db.Set<TimeSlot>().FirstOrDefault(tm => tm.Id == timeid);

            CourseInstance newcourseinstance = new CourseInstance();
            newcourseinstance.PreferredBy = currentfac;
            newcourseinstance.Course = course;
            newcourseinstance.Room = room;
            newcourseinstance.TimeSlot = timeslot;
            newcourseinstance.SectionNumber = 0;

            try
            {
                db.Set<CourseInstance>().Add(newcourseinstance);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static List<CourseInstance> GetPreferences(int departmentid, int facultyid, String UserName)
        {//get prefs for a facultymember -1 for current (blank or asp_username for current)
            List<CourseInstance> retlist = new List<CourseInstance>();
            if (UserName == "" || UserName == null) UserName=HttpContext.Current.User.Identity.Name;
            if (facultyid == -1 && UserName == HttpContext.Current.User.Identity.Name && !HttpContext.Current.User.IsInRole("admin"))//use current user
            {
                MembershipUser tempuser = Membership.GetUser(HttpContext.Current.User.Identity.Name);
                Database.Faculty currentfaculty;
                
                Department CurrentDepartment = ListOfDepartment.GetDepartment(ID: departmentid);
                currentfaculty = CurrentDepartment.GetFaculty((Guid)tempuser.ProviderUserKey);
                if (currentfaculty != null) facultyid = currentfaculty.Id;
                
            }
            else if (HttpContext.Current.User.IsInRole("admin"))
            {
                Database.Faculty currentfaculty;

                if (facultyid == -1 && UserName != HttpContext.Current.User.Identity.Name)
                {
                    Department CurrentDepartment = ListOfDepartment.GetDepartment(ID: departmentid);
                    currentfaculty = CurrentDepartment.GetFaculty(Guid.Empty, emailaddress: UserName);
                    if (currentfaculty != null) facultyid = currentfaculty.Id;
                }
                else if (facultyid != -1) { }
                else
                {
                    return retlist;
                }
                
            }
            else //block access from other faculty members
            {
                return retlist;
            }
            Model1Container db = new Model1Container();
            Faculty faculty = db.Set<Faculty>()
                .Include("PreferredInstances")
                .Include("PreferredInstances.Room")
                .Include("PreferredInstances.Course")
                .Include("PreferredInstances.TimeSlot")
                .FirstOrDefault(fac => fac.Id == facultyid);
            if (faculty == null) return retlist;

            foreach (CourseInstance ci in faculty.PreferredInstances)
            {

                retlist.Add(ci);
            }
            
            return retlist;
        }

        public static bool DeletePreference(CourseInstance preference)
        {
            Model1Container db = new Model1Container();
            CourseInstance courseinstance = db.Set<CourseInstance>().FirstOrDefault(ci => ci.Id == preference.Id);
            try
            {
                db.Set<CourseInstance>().Remove(courseinstance);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public string UserName
        {
            get
            {
                return this.ToString();
            }
        }
        public override string ToString()
        {
            if (Membership.GetUser(this.aspnet_userID) == null) return "orphan record no. " + this.Id;
            return Membership.GetUser(this.aspnet_userID).UserName;
        }
    }
}